﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClassLibrary1;


namespace CheckersGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Game game;

        public MainWindow()
        {
            InitializeComponent();

            game = new Game();



            Rectangle[,] r = new Rectangle[8, 8];
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                {
                    r[i, j] = new Rectangle();
                    r[i, j].Fill = (i + j) % 2 == 0 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black);
                    r[i, j].Width = 50;
                    r[i, j].Height = 50;
                    Canvas.SetTop(r[i, j], j * 50);
                    Canvas.SetLeft(r[i, j], i * 50);
                    canvas1.Children.Add(r[i, j]);
                }
            drawBoard(game.getBoard());
        }



        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            byte x = (byte)(e.GetPosition(canvas1).X / 50);
            byte y = (byte)(e.GetPosition(canvas1).Y / 50);

            if (x >= 0 && x < 8 && y >= 0 && y < 8)
            {
                bool selected, done;
                game.ClickSquare(Tuple.Create(x, y), out selected, out done, drawBoard, game.RedsTurn);
            }

            drawBoard(game.getBoard());
        }

        public void drawBoard(byte[,] board)
        {
            Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (canvas1.Children.Count > 64)
                    {
                        canvas1.Children.RemoveRange(64, canvas1.Children.Count - 64);
                    }

                    for (int i = 0; i < 8; i++)
                        for (int j = 0; j < 8; j++)
                        {
                            if (board[i, j] == Game.black)
                            {
                                Ellipse e = new Ellipse();

                                e.Height = 40;
                                e.Width = 40;

                                e.Stroke = new SolidColorBrush(Colors.LightGray);
                                e.Fill = new SolidColorBrush(Colors.Black);

                                Canvas.SetTop(e, j * 50 + 5);
                                Canvas.SetLeft(e, i * 50 + 5);


                                canvas1.Children.Add(e);
                            }
                            else if (board[i, j] == Game.blackKing)
                            {
                                Ellipse e = new Ellipse();

                                e.Height = 40;
                                e.Width = 40;

                                e.Stroke = new SolidColorBrush(Colors.Gold);
                                e.Fill = new SolidColorBrush(Colors.Black);

                                Canvas.SetTop(e, j * 50 + 5);
                                Canvas.SetLeft(e, i * 50 + 5);


                                canvas1.Children.Add(e);
                            }
                            else if (board[i, j] == Game.red)
                            {
                                Ellipse e = new Ellipse();

                                e.Height = 40;
                                e.Width = 40;

                                e.Stroke = new SolidColorBrush(Colors.LightGray);
                                e.Fill = new SolidColorBrush(Colors.Red);

                                Canvas.SetTop(e, j * 50 + 5);
                                Canvas.SetLeft(e, i * 50 + 5);


                                canvas1.Children.Add(e);
                            }
                            else if (board[i, j] == Game.redKing)
                            {
                                Ellipse e = new Ellipse();

                                e.Height = 40;
                                e.Width = 40;

                                e.Stroke = new SolidColorBrush(Colors.Gold);
                                e.Fill = new SolidColorBrush(Colors.Red);

                                Canvas.SetTop(e, j * 50 + 5);
                                Canvas.SetLeft(e, i * 50 + 5);


                                canvas1.Children.Add(e);
                            }
                        }
                }));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Game.ServerIP = ip.Text;
            Game.IsServer = (bool)isserver.IsChecked;
            game.connect(drawBoard);
        }

    }
}
